package com.diaohw.module.auth.service.listener;

import com.diaohw.platform.framework.authorization.OnlineManager;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@EnableScheduling
public class OnlineListener {

    @Resource
    private OnlineManager onlineManager;

    @Scheduled(cron = "0 0 0 * * ?")
    public void online() {
        long timeout = onlineManager.timeout();
        if (timeout <= 0) {
            return;
        }
        long max = System.currentTimeMillis() - timeout * 1000 * 2;

        Set<String> online = onlineManager.online(0, max);
        for (String s : online) {
            onlineManager.logout(s);
        }

    }

}
